package com.qingzhuge.member.controller;

import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.member.dto.request.SysUserMessageRequestDto;
import com.qingzhuge.member.dto.response.SysUserMessageResponseDto;
import com.qingzhuge.member.entity.SysMessageFormId;
import com.qingzhuge.member.entity.SysUserMessage;
import com.qingzhuge.member.service.ISysMessageFormIdService;
import com.qingzhuge.member.service.ISysUserMessageService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

import static com.qingzhuge.common.Constants.Controller.PATH_BY_ID;
import static com.qingzhuge.common.Constants.Controller.PATH_PAGE;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2018/9/25 0025 下午 17:36
 * @modified :
 * @since :
 */
@RestController
@RequestMapping("/api/auth/user/message")
@Api(tags = "A用户消息")
public class ApiMemberMessageController extends AbstractController {
    @Resource
    private ISysUserMessageService messageService;

    @Resource
    private ISysMessageFormIdService messageFormIdService;

    @ApiOperation(value = "前端分页")
    @PutMapping(PATH_PAGE)
    public ResponseEntity<ResponseBodyDto<PageUtil<SysUserMessageResponseDto>>> query(@RequestBody(required = false) SysUserMessageRequestDto messageDto) {
        SysUserMessage message = messageService.dto2Bean(messageDto);
        PageUtil<SysUserMessage> page = messageService.query(message);
        return returnSuccess(page,SysUserMessageResponseDto.class);
    }

    @ApiOperation(value = "详情")
    @GetMapping(PATH_BY_ID)
    public ResponseEntity<ResponseBodyDto<SysUserMessageResponseDto>> get(@PathVariable Long id) {
        SysUserMessage message = messageService.findById(id);
        return returnSuccess(message, SysUserMessageResponseDto.class);
    }

    @ApiOperation(value = "修改消息状态")
    @PutMapping(PATH_BY_ID)
    public ResponseEntity<ResponseBodyDto<SysUserMessageResponseDto>> update(@PathVariable Long id) {
        SysUserMessage message = new SysUserMessage(id);
        message.setRead(true);
        message = messageService.save(message);
        return returnSuccess(message, SysUserMessageResponseDto.class);
    }

    @ApiOperation(value = "删除消息")
    @DeleteMapping(PATH_BY_ID)
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        messageService.deleteById(id);
        return returnSuccess();
    }

    @ApiOperation(value = "保存消息模板id")
    @PutMapping("formId/" + PATH_BY_ID)
    public ResponseEntity<ResponseBodyDto<Object>> formId(
            @ApiParam("消息模板id")
            @PathVariable String id) {
        SysMessageFormId formId = new SysMessageFormId();
        formId.setUid(getUid());
        formId.setFormId(id);
        formId.setCreateTime(new Date());
        log.debug("formId {}", formId);
        messageFormIdService.save(formId);
        return returnSuccess();
    }
}
